2020-2021 Sunseeker Telemetry and Lighting System
eusci_b_i2c_ex1_masterRxMultiple.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 //******************************************************************************
63 //******************************************************************************
64 #include "driverlib.h"
65 
66 //*****************************************************************************
67 //
68 //Set the address for slave module. This is a 7-bit address sent in the
69 //following format:
70 //[A6:A5:A4:A3:A2:A1:A0:RS]
71 //
72 //A zero in the "RS" position of the first byte means that the master
73 //transmits (sends) data to the selected slave, and a one in this position
74 //means that the master receives data from the slave.
75 //
76 //*****************************************************************************
77 #define SLAVE_ADDRESS 0x48
78 
79 //*****************************************************************************
80 //
81 //Specify Expected Receive data count.
82 //
83 //*****************************************************************************
84 #define RXCOUNT 0x05
85 
86 //*****************************************************************************
87 //
88 //Target frequency for SMCLK in kHz
89 //
90 //*****************************************************************************
91 #define CS_SMCLK_DESIRED_FREQUENCY_IN_KHZ 1000
92 
93 //*****************************************************************************
94 //
95 //SMCLK/FLLRef Ratio
96 //
97 //*****************************************************************************
98 #define CS_SMCLK_FLLREF_RATIO 30
99 
100 uint8_t RXData;
101 void main (void)
102 {
103  WDT_A_hold(WDT_A_BASE);
104 
105  //Set DCO FLL reference = REFO
106  CS_initClockSignal(
107  CS_FLLREF,
108  CS_REFOCLK_SELECT,
109  CS_CLOCK_DIVIDER_1
110  );
111 
112  //Set Ratio and Desired MCLK Frequency and initialize DCO
113  CS_initFLLSettle(
116  );
117 
118  //Set SMCLK = DCO with frequency divider of 1
119  CS_initClockSignal(
120  CS_SMCLK,
121  CS_DCOCLKDIV_SELECT,
122  CS_CLOCK_DIVIDER_1
123  );
124 
125  //Set MCLK = DCO with frequency divider of 1
126  CS_initClockSignal(
127  CS_MCLK,
128  CS_DCOCLKDIV_SELECT,
129  CS_CLOCK_DIVIDER_1
130  );
131 
132  // Configure Pins for I2C
133  /*
134  * Select Port 5
135  * Set Pin 2, 3 to input with function, (UCB0SIMO/UCB0SDA, UCB0SOMI/UCB0SCL).
136  */
137  GPIO_setAsPeripheralModuleFunctionInputPin(
138  GPIO_PORT_P5,
139  GPIO_PIN2 + GPIO_PIN3,
140  GPIO_PRIMARY_MODULE_FUNCTION
141  );
142 
143  //Set P1.0 as an output pin.
144  /*
145 
146  * Select Port 1
147  * Set Pin 0 as output
148  */
149  GPIO_setAsOutputPin(
150  GPIO_PORT_P1,
151  GPIO_PIN0
152  );
153 
154  /*
155  * Disable the GPIO power-on default high-impedance mode to activate
156  * previously configured port settings
157  */
158  PMM_unlockLPM5();
159 
160  EUSCI_B_I2C_initMasterParam param = {0};
161  param.selectClockSource = EUSCI_B_I2C_CLOCKSOURCE_SMCLK;
162  param.i2cClk = CS_getSMCLK();
163  param.dataRate = EUSCI_B_I2C_SET_DATA_RATE_400KBPS;
164  param.byteCounterThreshold = RXCOUNT;
165  param.autoSTOPGeneration = EUSCI_B_I2C_SEND_STOP_AUTOMATICALLY_ON_BYTECOUNT_THRESHOLD;
166  EUSCI_B_I2C_initMaster(EUSCI_B0_BASE, &param);
167 
168  //Specify slave address
169  EUSCI_B_I2C_setSlaveAddress(EUSCI_B0_BASE,
171  );
172 
173  //Set Master in receive mode
174  EUSCI_B_I2C_setMode(EUSCI_B0_BASE,
175  EUSCI_B_I2C_RECEIVE_MODE
176  );
177 
178  //Enable I2C Module to start operations
179  EUSCI_B_I2C_enable(EUSCI_B0_BASE);
180 
181  EUSCI_B_I2C_clearInterrupt(EUSCI_B0_BASE,
182  EUSCI_B_I2C_RECEIVE_INTERRUPT0 +
183  EUSCI_B_I2C_BYTE_COUNTER_INTERRUPT +
184  EUSCI_B_I2C_NAK_INTERRUPT
185  );
186 
187  //Enable master Receive interrupt
188  EUSCI_B_I2C_enableInterrupt(EUSCI_B0_BASE,
189  EUSCI_B_I2C_RECEIVE_INTERRUPT0 +
190  EUSCI_B_I2C_BYTE_COUNTER_INTERRUPT +
191  EUSCI_B_I2C_NAK_INTERRUPT
192  );
193 
194  while (1)
195  {
196  __delay_cycles(2000);
197 
198  while (EUSCI_B_I2C_SENDING_STOP ==
199  EUSCI_B_I2C_masterIsStopSent(EUSCI_B0_BASE));
200 
201  EUSCI_B_I2C_masterReceiveStart(EUSCI_B0_BASE);
202 
203  // Enter LPM0 w/ interrupt
204  __bis_SR_register(CPUOFF+GIE);
205  }
206 }
207 
208 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
209 #pragma vector=USCI_B0_VECTOR
210 __interrupt
211 #elif defined(__GNUC__)
212 __attribute__((interrupt(USCI_B0_VECTOR)))
213 #endif
214 void USCIB0_ISR(void)
215 {
216  static uint8_t count = 0;
217  switch(__even_in_range(UCB0IV, USCI_I2C_UCBIT9IFG))
218  {
219  case USCI_NONE: // No interrupts break;
220  break;
221  case USCI_I2C_UCALIFG: // Arbitration lost
222  break;
223  case USCI_I2C_UCNACKIFG: // NAK received (master only)
224  EUSCI_B_I2C_masterReceiveStart(EUSCI_B0_BASE);
225  break;
226  case USCI_I2C_UCSTTIFG: // START condition detected with own address (slave mode only)
227  break;
228  case USCI_I2C_UCSTPIFG: // STOP condition detected (master & slave mode)
229  break;
230  case USCI_I2C_UCRXIFG3: // RXIFG3
231  break;
232  case USCI_I2C_UCTXIFG3: // TXIFG3
233  break;
234  case USCI_I2C_UCRXIFG2: // RXIFG2
235  break;
236  case USCI_I2C_UCTXIFG2: // TXIFG2
237  break;
238  case USCI_I2C_UCRXIFG1: // RXIFG1
239  break;
240  case USCI_I2C_UCTXIFG1: // TXIFG1
241  break;
242  case USCI_I2C_UCRXIFG0: // RXIFG0
243  // Get RX data
244  RXData = EUSCI_B_I2C_masterReceiveSingle(
245  EUSCI_B0_BASE
246  );
247  if (++count >= RXCOUNT) {
248  count = 0;
249  __bic_SR_register_on_exit(CPUOFF); // Exit LPM0
250  }
251  break; // Vector 24: RXIFG0 break;
252  case USCI_I2C_UCTXIFG0: // TXIFG0
253  break;
254  case USCI_I2C_UCBCNTIFG: // Byte count limit reached (UCBxTBCNT)
255  GPIO_toggleOutputOnPin(
256  GPIO_PORT_P1,
257  GPIO_PIN0
258  );
259  break;
260  case USCI_I2C_UCCLTOIFG: // Clock low timeout - clock held low too long
261  break;
262  case USCI_I2C_UCBIT9IFG: // Generated on 9th bit of a transmit (for debugging)
263  break;
264  default:
265  break;
266  }
267 }
268 
void USCIB0_ISR(void)
#define CS_SMCLK_FLLREF_RATIO
#define CS_SMCLK_DESIRED_FREQUENCY_IN_KHZ
MPU_initThreeSegmentsParam param
__bic_SR_register_on_exit(LPM3_bits|GIE)
__delay_cycles(500000)